---
title: "Java Annotation Processor"
date: 2020-3-11
categories:
- java
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org0ac0493">Classes</a>
<ul>
<li><a href="#org34635dc">Element</a></li>
</ul>
</li>
<li><a href="#org9dfaae9">maven项目传参</a></li>
<li><a href="#org35cbd8a">DEBUG</a>
<ul>
<li><a href="#orgebfb74a">在Idea中调试Annotation Processor</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-org0ac0493">
<h2 id="org0ac0493">Classes</h2>
<div class="outline-text-2" id="text-org0ac0493">
</div>
<div class="outline-3" id="outline-container-org34635dc">
<h3 id="org34635dc">Element</h3>
<div class="outline-text-3" id="text-org34635dc">
<p>
interface Element
</p>
<ul class="org-ul">
<li></li>
</ul>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org9dfaae9">
<h2 id="org9dfaae9">maven项目传参</h2>
<div class="outline-text-2" id="text-org9dfaae9">
<p>
与javac 命令的 -A 选项有关
</p>
<div class="org-src-container">
<pre class="src src-xml">&lt;<span style="font-weight: bold;">project</span>&gt;
    ...
    &lt;<span style="font-weight: bold;">build</span>&gt;
        &lt;<span style="font-weight: bold;">plugins</span>&gt;
            &lt;<span style="font-weight: bold;">plugin</span>&gt;
                &lt;<span style="font-weight: bold;">groupId</span>&gt;org.apache.maven.plugins&lt;/<span style="font-weight: bold;">groupId</span>&gt;
                &lt;<span style="font-weight: bold;">artifactId</span>&gt;maven-compiler-plugin&lt;/<span style="font-weight: bold;">artifactId</span>&gt;
                &lt;<span style="font-weight: bold;">configuration</span>&gt;
                    &lt;<span style="font-weight: bold;">source</span>&gt;1.8&lt;/<span style="font-weight: bold;">source</span>&gt;
                    &lt;<span style="font-weight: bold;">target</span>&gt;1.8&lt;/<span style="font-weight: bold;">target</span>&gt;
                    &lt;<span style="font-weight: bold;">encoding</span>&gt;UTF-8&lt;/<span style="font-weight: bold;">encoding</span>&gt;
                    &lt;<span style="font-weight: bold;">compilerArgs</span>&gt;
                        &lt;<span style="font-weight: bold;">arg</span>&gt;-Akey=value&lt;/<span style="font-weight: bold;">arg</span>&gt;
                        &lt;<span style="font-weight: bold;">arg</span>&gt;-Ake2=value2&lt;/<span style="font-weight: bold;">arg</span>&gt;
                    &lt;/<span style="font-weight: bold;">compilerArgs</span>&gt;
                &lt;/<span style="font-weight: bold;">configuration</span>&gt;
            &lt;/<span style="font-weight: bold;">plugin</span>&gt;
        &lt;/<span style="font-weight: bold;">plugins</span>&gt;
    &lt;/<span style="font-weight: bold;">build</span>&gt;
    ...
&lt;/<span style="font-weight: bold;">project</span>&gt;
</pre>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org35cbd8a">
<h2 id="org35cbd8a">DEBUG</h2>
<div class="outline-text-2" id="text-org35cbd8a">
</div>
<div class="outline-3" id="outline-container-orgebfb74a">
<h3 id="orgebfb74a">在Idea中调试Annotation Processor</h3>
<div class="outline-text-3" id="text-orgebfb74a">
<blockquote>
<p>
<a href="https://www.jianshu.com/p/4d5917f719f1">https://www.jianshu.com/p/4d5917f719f1</a>
</p>
</blockquote>
<ol class="org-ol">
<li>Idea菜单 Help - Edit Custom VM Options，添加下面的参数后重启Idea</li>
</ol>
<pre class="example">
-Dcompiler.process.debug.port=5005
</pre>
<ol class="org-ol">
<li>Run/Debug Configurations 中添加一个远程调试配置，注意
<ul class="org-ul">
<li>端口号与上一步配置的一致</li>
<li>Use module classpath 需要选择AnnotationProcessor所在的模块</li>
</ul></li>
</ol>
<div class="figure">
<p><img alt="idea-debug-config.png" src="idea-debug-config.png"/>
</p>
</div>
<ol class="org-ol">
<li>打开Idea的“调试构建”开关</li>
</ol>
<p>
双击shift，搜索 debug build process，将开关打开。这个开关在Idea重启后失效，因为大部分情况是不需要调试构建过程的
</p>
<ol class="org-ol">
<li>构建项目，状态栏显示“wait for debugger connecting on port…"，这时启动第一步中配置的调试，构建才能继续</li>
</ol>
<p>
不进断点的原因：
</p>
<ul class="org-ul">
<li>Annotation Processor在与项目不同的另一个maven模块中</li>
<li>更改了Annotation Processor代码后最好Clean再build annotation processor</li>
<li>更改了被处理的模块的代码，也最好最clean，再构建。大概是增量构建的原因。</li>
</ul>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2020-3-11</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
